using System;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// The Control used to Manage the <see cref="DMPropRule"/> for a particular <see cref="DMProperty"/>.
    /// This control contains a <see cref="BOGridAndEditorControlWin"/> for managing the PropRule.
    /// This control also contains the <see cref="DMPropRuleParameterControl"/> for managing the Individual values for the rule.
    /// </summary>
    public class DMPropRuleControl : UserControlWin, IFormControl, IBusinessObjectControl
    {
        private DMProperty _dmProperty;
        private readonly DMPropRuleParameterControl _dmPropRuleParameterControl;

        /// <summary>
        /// Gets and Sets the Control used to Edit the Parameters for this DMPRopRuleControl
        /// </summary>
        public IBusinessObjectControl PropRuleParameterControl
        {
            get { return _dmPropRuleParameterControl; }
        }

        public DMPropRuleControl(IControlFactory controlFactory)
        {
            if (controlFactory == null) throw new ArgumentNullException("controlFactory");

            _dmPropRuleParameterControl = new DMPropRuleParameterControl(controlFactory);// {Width = 275, Height = 135};

            BorderLayoutManager layoutManager = controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(_dmPropRuleParameterControl, BorderLayoutManager.Position.Centre);

            this.Enabled = false;
        }

        /// <summary>
        /// The <see cref="DMProperty"/> whose rules are being managed by this Control.
        /// </summary>
        public virtual DMProperty Property
        {
            get { return _dmProperty; }
            set
            {
                _dmProperty = value;
                if (_dmProperty == null)
                {
                    _dmPropRuleParameterControl.BusinessObject = null;
                    this.Enabled = false;
                    return;
                }
                _dmPropRuleParameterControl.BusinessObject = _dmProperty.DefaultPropRule;
                this.Enabled = true;
            }
        }

        ///<summary>
        /// Sets the form to control
        ///</summary>
        ///<param name="form">The form to control</param>
        public void SetForm(IFormHabanero form)
        {
        }

        /// <summary>
        /// gets and sets the Business object that this control is editing in this 
        /// case it is the parent object <see cref="DMProperty"/> since this control is being used to 
        /// edit the <see cref="DMPropRule"/>s for the <see cref="DMProperty"/>
        /// </summary>
        public IBusinessObject BusinessObject
        {
            get { return this.Property; }
            set { this.Property = (DMProperty) value; }
        }
    }
}